40
Beginner’s Guide to Code Algorithms
40
STEP 3
Check all rows.
For k = 1 To 9
If i <> k Then
‘ For m = 1 To 9
If sbox(k, j) > 0 Then
foundincantbelist = 0
For kk = 1 To 9
If sbox(k, j) = cantbelist(i, j, kk) Then foundincantbelist = 1
Next kk
If foundincantbelist = 0 Then
cantbelist(i, j, NextEmptyLocation(i, j)) = sbox(k, j)
End If
End If
‘ Next m
End If
Next k:
:
Next i
STEP 4
Now we need to examine each cell in the 3 by 3 matrix this cell belongs to. The
algorithm used to find the row and column values for the 3 by 3 grid is very simple.
Both for row and column, the technique is to find the closest multiple of 3 and then
add 1, 2, and 3 to it. The “int” function helps with this by removing the decimals.
For example, the multiple of 3 closest to 7 is 6. You can get that by running this
formula:
Int((i - 1) / 3) * 3 where i is 7.
For k = 1 To 3
n = Int((i - 1) / 3) * 3 + k
For l = 1 To 3
p = Int((j - 1) / 3) * 3 + l
If (l = n And j = p) Then
m = m ‘(do nothing)
Else
If sbox(n, p) <> ““ Then
foundincantbelist = 0
For kk = 1 To 9
If sbox(n, p) = cantbelist(i, j, kk) Then foundincantbelist = 1
Next kk
If foundincantbelist = 0 Then
cantbelist(i, j, NextEmptyLocation(i, j)) = sbox(n, p)
End If
End If
End If
Next l
Next k
Now that the “cantbelist” is built, we can simply check its contents. Any empty
cell with eight values (from 1 to 9) in “cantbelist” must be assigned the ninth digit.
This next code does exactly that.